package com.whut.welcomepage;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.whut.welcomepage.fragment.MainFragment;
import com.whut.welcomepage.fragment.WelcomeFragment;

public class MainActivity extends AppCompatActivity {


    private FragmentManager manager;
    private MainFragment mainFragment;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLoginInfo();
    }


    private void getLoginInfo() {
        preferences = getSharedPreferences("loginInfo", MODE_PRIVATE);
        boolean isFirst = preferences.getBoolean("isFirst", true);
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        mainFragment = new MainFragment();
        if (isFirst) {
            WelcomeFragment welcomeFragment = new WelcomeFragment();
            transaction.replace(R.id.layout_main, welcomeFragment);
        } else {
            transaction.replace(R.id.layout_main, mainFragment);
        }
        transaction.commit();
    }

    public void onClick(View view) {
//        preferences.edit().putBoolean("isFirst", false).commit();
        manager.beginTransaction().replace(R.id.layout_main, mainFragment).commit();
    }
}
